﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class admin_ncupdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int categoryid = 0;
            try{
                categoryid = Int32.Parse(Request.QueryString["cid"].ToString());
            }catch{
                categoryid = 0;
            }
            if(categoryid == 0){
                Response.Redirect("admin_newscategory.aspx");
            }
            if (!IsPostBack)
            {
                BindDate(categoryid);
            }
        }

        private void BindDate(int categoryid)
        {
            BindNewsCategory(categoryid);
            BindNotice();
        }
        private void BindNewsCategory(int CategoryID)
        {
            NewsCategoryInfo newscategory = new NewsCategoryInfo();
            newscategory = NewsCategoryInfoLogic.GetOneNewsCategoryInfo(CategoryID);
            txbTitle.Text = newscategory.CategoryName;
            lbCategoryID.Text = newscategory.CategoryID.ToString();
        }
        private void BindNotice()
        {
            noticefail.Visible = false;
            noticesuccess.Visible = false;
        }

        protected void btnSubmit_OnClick(object sender, EventArgs e)
        {
            int categoryid = 0;
            try
            {
                 categoryid = Int32.Parse(lbCategoryID.Text);
            }catch
            {
                ChangeStatus(false);
                return;
            }
            NewsCategoryInfo newscategory = NewsCategoryInfoLogic.GetOneNewsCategoryInfo(categoryid);
            newscategory.CategoryName = txbTitle.Text.Trim();
            int isSuccess = NewsCategoryInfoLogic.UpdateNewsCategoryInfo(newscategory);
            if (isSuccess == 1)
            {
                ChangeStatus(true);
            }else{
                ChangeStatus(false);
            }
        }

        private void ChangeStatus(bool flag)
        {
            noticesuccess.Visible = flag ? true : false;
            noticefail.Visible = flag ? false : true;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "initclosenotice", "initCloseNotice();", true);
        }
    }
}
